<?php
/*
 **************************************************************************************************
 *	Contains all of the theme functions that hook into core action / filter hooks,
 *	Other than theme setup functions, widget functions, and settings api functions
 **************************************************************************************************
*/

/*
 **************************************************************************************************
 *	Load google font
 **************************************************************************************************
*/
function prototype_google_font_url() {

	// query string
	$query_args = array(
		// add font name to your css -> font-family: 'Roboto', sans-serif;
		'family'	=> 'Roboto:300,400,500,700,300italic,400italic,500italic,700italic'
	);

	// generate url with query string
	return $fonts_url = add_query_arg( $query_args, "//fonts.googleapis.com/css" );

}

/*
 **************************************************************************************************
 *	Enqueues styles in header for front-end
 **************************************************************************************************
*/
function prototype_styles() {

	// main stylesheet
	wp_register_style( 'wp-style', get_stylesheet_uri() );
	wp_enqueue_style( 'wp-style' );

	// load google font
	wp_register_style( 'google-fonts', prototype_google_font_url() );
	wp_enqueue_style( 'google-fonts' );

	// color theme ( default )
	wp_register_style( 'color-style', get_template_directory_uri() . '/css/color/default.css' );
	wp_enqueue_style( 'color-style' );

	// pagenavi
	wp_register_style( 'pagenavi', get_template_directory_uri() . '/css/pagenavi.css' );
	wp_enqueue_style( 'pagenavi' );

}

// proper hook to use when enqueuing css items
add_action( 'wp_enqueue_scripts', 'prototype_styles' );

/*
 **************************************************************************************************
 *	Enqueues scripts in footer for front-end
 **************************************************************************************************
*/
function prototype_scripts() {

	// remove wordpress standard jquery ( used for load jquery in <footer> )
	wp_deregister_script( 'jquery' );

	// load jquery
	wp_register_script( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.8.3', true );
	wp_enqueue_script( 'jquery' );

	// load bootstrap
	wp_register_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.js', array( 'jquery' ), '2.3.2', true );
	wp_enqueue_script( 'bootstrap' );

	// load bxslider
	wp_register_script( 'bxslider', get_template_directory_uri() . '/js/bxslider.js', array( 'jquery' ), '4.1.1', true );
	wp_enqueue_script( 'bxslider' );

	// load pretty-photo
	wp_register_script( 'pretty-photo', get_template_directory_uri() . '/js/pretty-photo.js', array( 'jquery' ), '3.1.5', true );
	wp_enqueue_script( 'pretty-photo' );

	// load javascript custom
	wp_register_script( 'custom-js', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ), '1.0.0', true );
	wp_enqueue_script( 'custom-js' );

	// add javascript to pages with the comment form to support sites with threaded comments (when in use)
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

}

// proper hook to use when enqueuing js items
add_action( 'wp_enqueue_scripts', 'prototype_scripts' );

/*
 **************************************************************************************************
 *	Add IE conditional html5 shim to header
 **************************************************************************************************
*/
function prototype_html5_shim() {

	global $is_IE;

	if ($is_IE) {
		// loads html5 javascript file to add support for html5 elements in older IE versions
		echo '<!--[if lt IE 9]>';
		echo '<script src="' . get_template_directory_uri() . '/js/html5-shim.js" type="text/javascript"></script>';
		echo '<![endif]-->';
	}

}

// triggered within the <head></head> section
add_action( 'wp_head', 'prototype_html5_shim' );

/*
 **************************************************************************************************
 *	Font Options
 **************************************************************************************************
*/
function prototype_font_changed() {

	// option-tree framework
	if ( function_exists( 'ot_get_option' ) ) {

		$font_option = ot_get_option( 'sel_font' );
		
		// default font
		if ( empty( $font_option ) )
			$font_option = 'Arial,Helvetica,sans-serif';

		// render style to <head></head>
		echo '<style type="text/css">body { font-family:' . $font_option . '}</style>';

	}

}

// triggered within the <head></head> section
add_action( 'wp_head', 'prototype_font_changed' );

/*
 **************************************************************************************************
 *	Creates a nicely formatted and more specific title element text
 **************************************************************************************************
*/
function prototype_title( $title, $separator ) {

	// $paged contains the page number of a listing of posts
	// $page contains the page number of a single post that is paged
	global $paged, $page;

	// do not affect wp_title() calls in feeds
	if ( is_feed() )
		return $title;

	// add the site name
	$title .= get_bloginfo( 'name' );

	// add the site description for the home / front page
	$site_description = get_bloginfo( 'description', 'display' );

	if ( $site_description && ( is_home() || is_front_page() ) )
		$title = "$title $separator $site_description";

	// add a page number if on page 2 or more
	if ( $paged >= 2 || $page >= 2 )
		$title = "$title $separator " . sprintf( __( 'Page %s', TEXTDOMAIN ), max( $paged, $page ) );

	// return the new title to wp_title()
	return $title;

}

// filters the text appearing in the HTML <title> tag
add_filter( 'wp_title', 'prototype_title', 10, 2 );

/*
 **************************************************************************************************
 *	Add additional css classes for the body_classes
 **************************************************************************************************
*/
function prototype_additional_body_class( $classes ) {

	// add custom class to the $classes array
	$classes[] = '';
	// return the $classes array
	return $classes;

}

// filters the body_class
add_filter('body_class', 'prototype_additional_body_class');

/*
 **************************************************************************************************
 *	Add additional css classes for the post_classes
 **************************************************************************************************
*/
function prototype_additional_post_class( $classes ) {

	// add custom class to the $classes array
	if ( is_single() )
		$classes[] = 'single';
	// return the $classes array
	return $classes;

}

// filters the post_class
add_filter('post_class', 'prototype_additional_post_class');

/*
 **************************************************************************************************
 *	Register sidebar (appearance -> widgets)
 **************************************************************************************************
*/
function prototyp_widgets_init() {

	// register the primary sidebar
	register_sidebar( array(
		'name'          => __( 'Primary Sidebar', TEXTDOMAIN ),
		'id'            => 'primary-sidebar',
		'description'   => __( 'Appears in the right section of the site', TEXTDOMAIN ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h5 class="widget-title widgetheading">', // widgetheading is the custom class
		'after_title'   => '</h5>',
	) );

}

add_action( 'widgets_init', 'prototyp_widgets_init' );

/*
 **************************************************************************************************
 *	Custom comment form
 **************************************************************************************************
*/
/*
 *-------------------------------------------------------------------------------------------------
 *	Overwrite arguments other than the default fields
 *-------------------------------------------------------------------------------------------------
*/
function prototype_comment_form_defaults( $defaults ) {

	$defaults['id_form'] = 'commentform';
	$defaults['id_submit'] = 'submit';

	$defaults['label_submit'] = __( 'Post Comment', TEXTDOMAIN );
	$defaults['title_reply'] = __( 'Leave a Reply', TEXTDOMAIN );
	$defaults['cancel_reply_link'] = __( 'Cancel Reply', TEXTDOMAIN );

	$defaults['comment_field'] = '<p class="comment-form-comment">' .
								 '<label for="comment">' . _x( 'Comment', 'noun', TEXTDOMAIN ) . ' <span class="required">*</span></label>' .
								 '<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>' .
								 '</p>';
	return $defaults;

}

add_filter( 'comment_form_defaults', 'prototype_comment_form_defaults' );

/*
 *-------------------------------------------------------------------------------------------------
 *	Change the output of the fields argument or add extra inputs
 *-------------------------------------------------------------------------------------------------
*/
function prototype_comment_form_default_fields( $fields ) {

	// must first set these variables
	$commenter = wp_get_current_commenter();
	$req = get_option( 'require_name_email' );
	$aria_req = ( $req ? " aria-required='true'" : '' );

	// custom fields
	$fields['author'] = '<p class="comment-form-author">' .
						'<label for="author">' . __( 'Name', TEXTDOMAIN ) . ' ' . ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
						'<input id="author" name="author" type="text" placeholder="" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' />' .
						'</p>';

	$fields['email'] = '<p class="comment-form-email">' .
					   '<label for="email">' . __( 'Email', TEXTDOMAIN ) . ' ' . ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
					   '<input id="email" name="email" type="text" placeholder="" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />' .
					   '</p>';

	$fields['url'] = '<p class="comment-form-url">' .
					 '<label for="url">' . __( 'Website', TEXTDOMAIN ) . '</label> ' .
					 '<input id="url" name="url" type="text" placeholder="" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30"' . $aria_req . ' />' .
					 '</p>';

	return $fields;

}

add_filter( 'comment_form_default_fields', 'prototype_comment_form_default_fields' );